<?php

class Soul_Template_Html_Grid extends Soul_Template_Html_Container {

	private $_title = '';
    private $_dataSource = NULL;
    private $_primaryKey = '';
    private $_sortField = '';
    private $_sortDirection = 'asc';
    private $_sortUrlFormat = '';
    private $_rowIndex = -1;
    private $_rowCount = 0;
    private $_hasBody = FALSE;
    private $_hasHead = FALSE;
    private $_hasFoot = FALSE;

    public function __construct() {
        $this->addClass('grid');
    }
    
    public function getTitle(){
    	return $this->_title;
    }
    
    public function setTitle($value){
    	$this->_title = $value;
    	return $this;
    }

    public function getDataSource() {
        return $this->_dataSource;
    }

    public function setDataSource($value) {
        $this->_dataSource = $value;
        $this->_rowCount = count($this->_dataSource);
        return $this;
    }

    public function getPrimaryKey() {
        return $this->_primaryKey;
    }

    public function setPrimaryKey($value) {
        $this->_primaryKey = $value;
        return $this;
    }

    public function getSortField() {
        return $this->_sortField;
    }

    public function setSortField($value) {
        $this->_sortField = $value;
        return $this;
    }

    public function getSortDirection() {
        return $this->_sortDirection;
    }

    public function setSortDirection($value) {
        $this->_sortDirection = $value;
        return $this;
    }

    public function getSortUrlFormat() {
        return $this->_sortUrlFormat;
    }

    public function setSortUrlFormat($value) {
        $this->_sortUrlFormat = $value;
        return $this;
    }

    public function resetRowIndex() {
        $this->_rowIndex = -1;
        return $this;
    }

    public function isFirstRow() {
        return $this->_rowIndex == 0;
    }

    public function isLastRow() {
        return $this->_rowIndex + 1 == $this->getRowCount();
    }

    public function isEvenRow() {
        return $this->_rowIndex % 2 != 0;
    }

    public function isOddRow() {
        return!$this->isEvenRow();
    }

    public function getRowCount() {
        return $this->_rowCount;
    }

    public function getRowIndex() {
        return $this->_rowIndex;
    }

    public function getCurrentRow() {
        return $this->_dataSource[$this->_rowIndex];
    }

    public function nextRow() {
        $this->_rowIndex++;
        if ($this->_rowIndex >= $this->getRowCount()) {
            return FALSE;
        }
        return $this->getCurrentRow();
    }

    public function resetDataSource() {
        $this->_rowIndex = -1;
        return $this;
    }
    
    public function renderHeadBegin(){
    	$this->_hasHead = true;
    	return '<thead>';
    }
    
    public function emptyHead(){
		return new Soul_Template_Html_Grid_Head_Empty();
    }
    
    public function customHead(){
		return new Soul_Template_Html_Grid_Head_Custom();
    }    
       
    public function labelHead($text, $sortField = NULL){
    	$result = new Soul_Template_Html_Grid_Head_Label();
    	return $result->setText($text)->setSortField($sortField)->setGrid($this);
    }
    
    public function htmlHead($html){
    	$result = new Soul_Template_Html_Grid_Head_Html();
    	$result->setContent($html);
    	return $result;
    }
    
    public function renderHeadEnd(){
    	return '</thead>';
    }
    
    public function renderRowBegin(){
    	$class = $this->isEvenRow()? 'even': 'odd';
    	if(!$this->_hasBody){
    		$this->_hasBody = TRUE;
    		return '<tbody><tr class="'.$class.'">';
    	}
    	return '<tr class="'.$class.'">';
    }
    
    public function emptyCell(){
    	$result = new Soul_Template_Html_Grid_Cell_Empty();
    	return $result->setGrid($this);
    }

    public function indexCell(){
    	$result = new Soul_Template_Html_Grid_Cell_Index();
    	return $result->setGrid($this);
    }
    
    public function checkboxCell($value, $name = ''){
    	$result = new Soul_Template_Html_Grid_Cell_Checkbox();
    	return $result->setValue($value)->setName($name)->setGrid($this);
    }    
       
    public function labelCell($text, $emptyText = ''){
    	$result = new Soul_Template_Html_Grid_Cell_Label();
    	return $result->setText($text)->setEmptyText($emptyText)->setGrid($this);
    }
    
    public function linkCell($text, $url, $target = ''){
    	$result = new Soul_Template_Html_Grid_Cell_Link();
    	return $result->setText($text)->setUrl($url)->setTarget($target)->setGrid($this);
    }    
    
    public function datetimeCell($text, $format = 's'){
    	$result = new Soul_Template_Html_Grid_Cell_Datetime();
    	return $result->setText($text)->setFormat($format)->setGrid($this);
    }
    
    public function customCell(){
		return new Soul_Template_Html_Grid_Cell_Custom();
    }
    
    public function renderRowEnd(){
    	return '</tr>';
    }
    
    public function renderFootBegin(){
    	$this->_hasFoot = true;
        if($this->_hasBody){
    		return '</tbody></tfoot>';
    	}    	
    	return '<tfoot>';
    }
       
    public function sumFoot($type = 'Soul_Template_Html_Grid_Foot_Sum'){
    	$result = new $type();
    	return $result->setGrid($this);
    }
    
    public function renderFootEnd(){
    	return '</tfoot>';
    }    
    
    public function renderBegin(){
    	return '<table'.$this->getAttributeHtml().'>';
    }
    
    public function renderEnd(){
    	if(!$this->_hasFoot && $this->_hasBody){
    		return '</tbody></table>';
    	}
    	return '</table>';
    }
    
    public function renderChildren(){
    	return '';
    }
}